/**********************
 * 
 * Main.js
 * 
 **********************/

// global variable to store blog posts
var blogposts = new Array();

// global variable to store blog_users
var blogusers = new Array();

// global variable to store the current active user
var currentUser = "51158405";

/*
 * Object constructor to represent a single blog post
 * @method blogpost
 * @param {String}id id of a post
 * @param {String}author author of a post
 * @param {String}title title of a post
 * @param {String}content content of a post
 * @param {String}date updated date of a post
 * @param {String}time updated time of a post
 */
function blogpost(id, author, title, content, date, time) {
	this.id = id;
	this.author = author;
	this.title = title;
	this.content = content;
	this.date = date;
	this.time = time;
}


/**
 * Method to clear the create form
 * @method clearForm

function clearForm() {
	$("#create_user").val("");
	$("#create_pw").val("");
	$("#create_title").val("");
	$("#create_content").val("");
} */

/**
 * Method of handling form validation
 * @method validateForm
 * @return {Boolean}result true if form is validated
 */
function validateForm() {
	var msg = "";
	var result = true;
	
	if( $("#create_user").val() == "" ) {
		msg += "User cannot be empty.\n";
	}
	if( !$("#create_pw").val() ) {
		msg += "Password cannot be empty.\n";
	}
	if( !$("#create_title").val() ) {
		msg += "Title cannot be empty.\n";
	}
	if( !$("#create_content").val() ) {
		msg += "Content cannot be empty.\n";
	}
	if( msg != "" ) {
		alert(msg);
		result = false;
	}
	return result;
}


/**
 * popup create form when create button clicked
 * @method popupCreate

function popupCreate() {
	
	$("#create").click(function(e) {
		// cancel the link behavior
		e.preventDefault();
		
		$(this).colorbox({inline:true, width:"50%"});
		
	});
} */

/**
 * Pop up an edit form and filled with the content of selected blog entry
 * triggered when a Edit button is clicked
 * @method popupEdit
 
function popupEdit() {
	
	$(".edit_btn").click(function(e) {
		// cancel the link behavior
		e.preventDefault();
		
		$(this).colorbox({inline:true, width:"50%"});
			
		//get the id of blog to be edit
		var blog_id = $(this).attr('rel');
		
		// get the author, title, and content
		//var author = $("#" + blog_id).find(".author").find("span").html();
		var author = currentUser;
		var title = $("#" + blog_id).find(".title").html();
		var content = $("#" + blog_id).find(".content").html();
		
		$("#edit_user").val(author);
		$("#edit_title").val(title);
		$("#edit_content").val(content);
		
	});
	
}*/

/**
 * Show a password field for user to input
 * triggered when a Delete button is clicked
 * @method popupDelete

function popupDelete() {
	
	$(".delete_btn").click(function(e) {
		// cancel the link behavior
		e.preventDefault();

		$(this).colorbox({inline:true, width:"50%"});
		
		//get the id of blog to be edit
		var blog_id = $(this).attr('rel');
		
		// get the author, title, and content
		//var author = $("#" + blog_id).find(".author").find("span").html();
		var author = currentUser;
		
		$("#delete_user").val(author);

	});
} */

/**
 * To close the popup edit window, triggered when submit or cancel button is clicked
 * @method closeEdit

function closeEdit() {
	$("#edit_cancel").click(function() {
		$("#mask, #edit_popup").hide();		
	});
} */

/**
 * all event handlers
 * @method eventHandler

function eventHandler() {
	$("#create_submit").click(createPost);
	$("#create_cancel").click(clearForm);	
	$("#edit_form").submit(editPost);
	$("#delete_form").submit(deletePost);
	
	// when select a user, clean the page and show content accordingly
	$("#blog_users").change(function(){
		$("#content").html("");
		$("#loading").show();
		getPosts($('#blog_users').val());
	});

} */

/**
 * activate the Edit and Delete button of each post after all posts are rendered
 * @method activateButton

function activateButton() {
	popupEdit();
	popupDelete();
} */


/**
 * Method to be triggered when page is loaded
 * @method setup
 */
function setup() {
	getBloggers();
	getPosts(currentUser);
	//changeMenu();
	eventHandler(); 
	yui_calendar();
	//yui_autocomplete();
}

$(document).ready(setup);